package com.xmkj.carwash.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.common.adapter.helper.IRecyclerViewHelper;
import com.common.carbean.CarListBean;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.methods.CarListMethods;
import com.common.retrofit.methods.DelCarMethods;
import com.common.retrofit.methods.SetDefaultCarMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.rxbus.RxBus;
import com.common.rxbus.postevent.RxKeyEvent;
import com.common.utils.EmptyUtils;
import com.common.utils.SizeUtils;
import com.common.widget.DividerDecoration.MarginDecoration;
import com.common.widget.recyclerview.refresh.adapter.CommonAdapter;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.xmkj.carwash.R;
import com.xmkj.carwash.mine.adapter.ChooseCarAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.functions.Action1;

/**
 * Created by Administrator on 2018/4/7.
 */

public class ChooseCarActivity extends BaseMvpActivity {
    @BindView(R.id.recyclerview)
    XRecyclerView recyclerview;
    private List<CarListBean.ListsEntity> bean = new ArrayList<>();
    private ChooseCarAdapter adapter;
    private CarListBean.ListsEntity infobean;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_car_info;
    }

    @Override
    protected void onViewCreated() {
//        setRightNavigationText();
    }


    @Override
    protected void doLogicFunc() {
        rxManager.add(RxBus.getDefault().toObservable(RxKeyEvent.class).subscribe(new Action1<RxKeyEvent>() {
            @Override
            public void call(RxKeyEvent rxKeyEvent) {
                if (EmptyUtils.isNotEventEmpty(rxKeyEvent, RxKeyEvent.ADDCAR)&&(Boolean) rxKeyEvent.getValue()) {
                    gotoHttp();

                }
            }
        }));
        gotoHttp();
        setRecyclerView();
    }


    private void setRecyclerView() {

        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setHasFixedSize(true);
        recyclerview.setRefreshing(false);
        adapter = new ChooseCarAdapter(context, bean);
        recyclerview.setAdapter(adapter);

        adapter.setOnItemChildClickListener(new CommonAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(CommonAdapter adapter, View view, int position) {
                infobean = (CarListBean.ListsEntity) adapter.mData.get(position);
                if (view.getId() == R.id.rl_choose) { //删除
                    RxBus.getDefault().postSticky(new RxKeyEvent(RxKeyEvent.NOW_CHOOSE_CAR, infobean));
                    onBackPressed();

                } else if (view.getId() == R.id.tv_set_default) { //设置默认
                    gotoSetDefault(infobean.id);
                }
            }
        });
        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                gotoHttp();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                gotoHttp();
            }
        });
    }

    private void gotoSetDefault(int id) {
        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                showToastMsg("设置成功");
                mPageIndex = 1 ;
                gotoHttp();
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });
        SetDefaultCarMethods.getInstance().setDefault(subscriber, uid, hashid, id );
        rxManager.add(subscriber);
    }

    private void gotoDelete(final int position, int id) {
        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                adapter.remove(position);
                bean.remove(position);
                showToastMsg("删除成功");

//                gotoHttpRep();
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });
        DelCarMethods.getInstance().carDel(subscriber, uid, hashid, id );
        rxManager.add(subscriber);
    }

    private void gotoHttp() {
        CommonSubscriber<CarListBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                CarListBean homeBean = (CarListBean) o;
                List<CarListBean.ListsEntity> beanList = homeBean.lists;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    adapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    bean = beanList;
                    adapter.addAll(bean);
                    statusContent();
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }


            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });
        CarListMethods.getInstance().carList(subscriber,uid,hashid, mPageIndex,"");
        rxManager.add(subscriber);
    }


    @Override
    protected void setNavigation() {
        setNavigationBack("选择车辆");
    }



    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }
}
